import asyncio
import ygg_aiomysql as  aiomysql

async def main():
    """
    主函数: 演示数据查询完整流程
    """
    # 创建数据库连接池
    pool = await aiomysql.new_pool_env()
    
    try:
        # 创建测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("""
                    CREATE TABLE IF NOT EXISTS test_table (
                        id INT AUTO_INCREMENT PRIMARY KEY,
                        name VARCHAR(100),
                        age INT
                    )
                """)
                await conn.commit()
        
        # 先插入测试数据
        test_data = {'name': '示例用户', 'age': 25}
        inserted_id = await aiomysql.add(pool, 'test_table', test_data)
        print(f"已插入数据，ID: {inserted_id}")
        
        # 查询数据
        result = await aiomysql.get(pool, 'test_table', inserted_id)
        print(f"查询结果: {result}")
        
        # 验证查询结果
        print(f"姓名: {result['name']}")
        print(f"年龄: {result['age']}")
        print(f"ID: {result['id']}")
        
    finally:
        # 清理测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("DROP TABLE IF EXISTS test_table")
                await conn.commit()
        
        # 关闭连接池
        pool.close()
        await pool.wait_closed()

if __name__ == '__main__':
    asyncio.run(main())